package com.tzpay.tzpay.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tzpay.tzpay.R;
import com.tzpay.tzpay.bean.OrderInfo;
import com.tzpay.tzpay.bean.UserInfo;
import com.tzpay.tzpay.common.ConstValue;
import com.tzpay.tzpay.presenter.LoginLogicView;
import com.tzpay.tzpay.presenter.LoginPresenter;
import com.tzpay.tzpay.presenter.TradeLogicView;
import com.tzpay.tzpay.presenter.TradePresenter;
import com.tzpay.tzpay.ui.BaseFragment;
import com.tzpay.tzpay.ui.activity.TradeListActivity;
import com.tzpay.tzpay.utils.SPUtil;
import com.tzpay.tzpay.utils.SnackBarUtils;
import com.tzpay.tzpay.utils.Toaster;
import com.tzpay.tzpay.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenfeiyue on 16/4/11.
 */
public class TradeDetailFragment extends BaseFragment implements TradeLogicView {

    @Bind(R.id.tv_amount)
    TextView tvAmount;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_custom_id)
    TextView tvCustomId;
    @Bind(R.id.tv_status)
    TextView tvStatus;
    @Bind(R.id.tv_no)
    TextView tvNo;
    @Bind(R.id.tv_err_msg)
    TextView tvErrMsg;
    @Bind(R.id.button)
    TextView button;
    @Bind(R.id.container)
    CoordinatorLayout container;
    @Bind(R.id.progress)
    ProgressBar progress;
    @Bind(R.id.password)
    EditText tvPassWord;
    @Bind(R.id.layout_password)
    LinearLayout layoutPassword;
    private OrderInfo info;

    private TradePresenter tradePresenter;

    private LoginPresenter loginPresenter;

    public static TradeDetailFragment instance(OrderInfo info) {
        TradeDetailFragment fragment = new TradeDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("order_info", info);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_trade_detail, null);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            info = bundle.getParcelable("order_info");
        }

        if (info == null) {
            SnackBarUtils.showLongSnackBar(container, getString(R.string.data_err));
            finishActivity();
        }

        String amount = info.getOrderAmt();
        if (!TextUtils.isEmpty(amount)) {
            tvAmount.setText(getString(R.string.text_rmb) + Utils.getDotNumber(amount));
        }
        tvStatus.setText(info.getTradeStatus());
        tvNo.setText(info.getOrderNo());
        tvTime.setText(Utils.formatTime(info.getOrderDate()));

        String customId = info.getCustomerId();
        tvCustomId.setText(TextUtils.isEmpty(customId) ? "" : customId);

        String errMsg = info.getErrorMsg();

        if (!TextUtils.isEmpty(errMsg)) {
            tvErrMsg.setVisibility(View.VISIBLE);
            tvErrMsg.setText(errMsg);
            button.setVisibility(View.GONE);
        }
        button.setVisibility(info.isCanRefund() ? View.VISIBLE : View.GONE);
    }

    @OnClick(R.id.button)
    public void onButtonClick() {

        if (layoutPassword.getVisibility() == View.GONE) {
            layoutPassword.setVisibility(View.VISIBLE);
        } else {
            login();
        }

    }

    @Override
    public void showProgress(int type) {
        shopProgressBar(true);
    }

    @Override
    public void onFailed(int type, String resperr) {
        shopProgressBar(false);
        SnackBarUtils.showLongSnackBar(container, resperr);
    }

    @Override
    public void onSuccess(int type, Object result) {
        shopProgressBar(false);
        Toaster.l(getActivity(), getString(R.string.operate_sucess));
        // 刷新流水
        TradeListActivity.isNeadRefresh = true;
        finishActivity();
    }

    private void login() {
        if (loginPresenter == null) {
            LoginCallBack loginCallBack = new LoginCallBack();
            loginPresenter = new LoginPresenter(loginCallBack);
        }
        String userName = UserInfo.getInstance().getUserName();
        String password = tvPassWord.getText().toString();
        boolean isRememberPass = SPUtil.getInstance(getActivity()).getBoolean(ConstValue.Sp.REMENBER_PASS_CHECKED, true);
        loginPresenter.login(userName, password, isRememberPass);
    }

    private class LoginCallBack implements LoginLogicView {

        @Override
        public void renderLasLoginUserInfo(String username, String password, boolean isRememberPass) {
        }

        @Override
        public void setUsernameError() {
            shopProgressBar(false);
        }

        @Override
        public void setPasswordError() {
            shopProgressBar(false);
            tvPassWord.requestFocus();
            tvPassWord.setError(getString(R.string.password_error));
        }

        @Override
        public void showProgress() {
            shopProgressBar(true);
        }

        @Override
        public void onFailed(String resperr) {
            shopProgressBar(false);
            SnackBarUtils.showLongSnackBar(container, resperr);
        }

        @Override
        public void onSuccess(Object result) {
            // 登录成功，撤销交易
            orderRefund();
        }

        @Override
        public Context getContext() {
            return TradeDetailFragment.this.getActivity();
        }
    }

    private void shopProgressBar(boolean isShow) {
        progress.setVisibility(isShow ? View.VISIBLE : View.GONE);
        button.setEnabled(!isShow);
    }

    /**
     * 撤销交易
     */
    private void orderRefund() {
        if (tradePresenter == null) {
            tradePresenter = new TradePresenter(TradeDetailFragment.this);
        }

        String orderNo = info.getOrderNo();
        String orderReqNo = info.getOrderReqNo();
        String transAmt = info.getOrderAmt();
        tradePresenter.orderRefund(orderNo, orderReqNo, transAmt);
    }

}